package com.doit.day01;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.sql.*;
import java.time.Duration;
import java.util.Arrays;
import java.util.Properties;

/**
 * 创建一个topic mysql   4个分区   1个副本
 * 写一个消费者，消费这个topic中的数据  ==》 将数据写入到mysql数据库中
 * 表  user_info
 * 将数据写入到mysql中
 * 并且将偏移量也提交到mysql中
 * 你需要搞一张表，这张表用来记录偏移量 =-=》这张表怎么设计  group_id topic partition offset
 * 开启事务==》 把那两个动作绑定成一个事务  逻辑处理
 * 还要写一段代码 ==》 将offset 提交到mysql中  往mysql的表里面插入数据
 */
public class _01_精准一次性消费 {
    public static final String GROUP_ID = "g02";
    public static void main(String[] args) throws Exception {


        /**
         * 获取jdbc连接对象
         */
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
        conn.setAutoCommit(false);

        PreparedStatement pps = conn.prepareStatement("insert into user_info values(?,?,?,?)");

        //提交偏移量的sql
        PreparedStatement pps_of = conn.prepareStatement("insert tp_offset values(?,?,?,?) on duplicate key update offset = ?");

        PreparedStatement get_offset = conn.prepareStatement("select `partition` ,`offset` from tp_offset where group_id = ? and topic = ?");

        /**
         * 创建一个kafka消费者对象
         */
        //在shell客户端创建一个topic
        Properties props = new Properties();
        //设置必要的参数
        props.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"linux01:9092");
        props.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class.getName());
        props.setProperty(ConsumerConfig.GROUP_ID_CONFIG,GROUP_ID);

        //选配的
        props.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");
        props.setProperty(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,"false");
//        props.setProperty(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG,"10000");

        //搞一个消费者
        KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(props);

        //订阅主题 subscribe 凑代码当量
//        consumer.subscribe(Arrays.asList("mysql"));

        TopicPartition topicPartition = new TopicPartition("mysql", 0);
        TopicPartition topicPartition1 = new TopicPartition("mysql", 1);
        TopicPartition topicPartition2 = new TopicPartition("mysql", 2);
        TopicPartition topicPartition3 = new TopicPartition("mysql", 3);

        consumer.assign(Arrays.asList(topicPartition,topicPartition1,topicPartition2,topicPartition3));

        //表里面记录的位置我需要查出来

        get_offset.setString(1,GROUP_ID);
        get_offset.setString(2,"mysql");

        ResultSet resultSet = get_offset.executeQuery();
        while (resultSet.next()){
            int partition = resultSet.getInt("partition");
            long offset = resultSet.getLong("offset");
//            System.out.println(partition+","+offset);
            if (partition == 0){
                consumer.seek(topicPartition,offset+1);
//                System.out.println("0号分区的偏移量设置好了");
            }else if (partition == 1){
                consumer.seek(topicPartition1,offset+1);
//                System.out.println("1号分区的偏移量设置好了");
            }else if (partition == 2){
                consumer.seek(topicPartition2,offset+1);
//                System.out.println("2号分区的偏移量设置好了");
            }else if (partition == 3){
                consumer.seek(topicPartition3,offset+1);
//                System.out.println("3号分区的偏移量设置好了");
            }
        }

        //poll数据
        while (true){
            //拉取数据  获取数据是一批一批的获取的
            ConsumerRecords<String, String> poll = consumer.poll(Duration.ofMillis(Long.MAX_VALUE));
            for (ConsumerRecord<String, String> consumerRecord : poll) {
                try {
                    /**
                     * 业务数据逻辑的处理   一条一条拿出来的   把数据写入到类似于redis中
                     */
                    //1,zss,18,male
                    String value = consumerRecord.value();
                    String[] arr = value.split(",");
                    //字段的解析
                    int id = Integer.parseInt(arr[0]);
                    String name = arr[1];
                    int age = Integer.parseInt(arr[2]);
                    String gender = arr[3];
                    //往mysql数据库中写入数据  =》 获取mysql的jdbc驱动  获取到连接对象
                    //给上面的sql ? 设置值
                    pps.setInt(1,id);
                    pps.setString(2,name);
                    pps.setInt(3,age);
                    pps.setString(4,gender);
                    //开始执行
                    pps.execute();

                    /**
                     * 手动提交偏移量
                     */
                    String topic = consumerRecord.topic();
                    int partition = consumerRecord.partition();
                    long offset = consumerRecord.offset();
                    //还要将offset这些数据插入到mysql数据库中  手动提交偏移量
                    pps_of.setString(1,GROUP_ID);
                    pps_of.setString(2,topic);
                    pps_of.setInt(3,partition);
                    pps_of.setLong(4,offset);
                    pps_of.setLong(5,offset);
                    pps_of.execute();

                    //提交事务
                    conn.commit();
                } catch (Exception e) {
                    //回滚事务
                    conn.rollback();
                    System.out.println(e);
                }


            }

        }

        //for  consumerrecore.value ==> 1,zss,18,male ==> split 切割   写入到mysql数据库中

    }
}
